package com.lc.cehui.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.IndexApi;
import com.lc.cehui.http.request.IndexBean;
import com.lc.cehui.http.request.MyStaffListApi;

import com.lc.cehui.http.response.StaffListBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.adapter.ImageAdapter;
import com.lc.cehui.util.SPUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.listener.OnBannerListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 企业员工管理
 */
public class StaffManageActivity extends AppActivity {


    @BindView(R.id.et_seach)
    EditText etSeach;
    @BindView(R.id.car_recycle)
    RecyclerView mRecyclerView;
    @BindView(R.id.rl_status_refresh)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.top)
    TitleBar top;
    private List<StaffListBean.DataBean> list = new ArrayList<>();
    private BaseQuickAdapter adapter;
private  int mPage=1;
    private String flag;

    @Override
    protected int getLayoutId() {
        return R.layout.staffmanage_activity;
    }

    @Override
    protected void initView() {

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseQuickAdapter<StaffListBean.DataBean, BaseViewHolder>(R.layout.staff_item, list) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, StaffListBean.DataBean bean) {
                ImageView img = (ImageView) baseViewHolder.getView(R.id.img_dianpu);
                TextView name = (TextView) baseViewHolder.getView(R.id.name_tv);
                TextView job = (TextView) baseViewHolder.getView(R.id.job_tv);
                name.setText(bean.getName());
                job.setText(bean.getPosition());
                GlideApp.with(StaffManageActivity.this)
                        .load(IntentKey.PAGEURL+bean.getPicurl())
                        .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                        .into(img);
                baseViewHolder.getView(R.id.edit_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(StaffManageActivity.this,CreateStaffActivity.class).putExtra("id",bean.getId()+"").putExtra("name",bean.getName()).putExtra("mobile",bean.getMobile()).putExtra("picurl",bean.getPicurl()).putExtra("job",bean.getPosition()).putExtra("type","1").putExtra("password",""));
                    }
                });
            }
        };
        mRecyclerView.setAdapter(adapter);
        top.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
                startActivity(CreateStaffActivity.class);
            }
        });

        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                mPage = 1;
                list.clear();
                my_staff_list();

                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {



                if (flag.equals("T")) {
                    mPage++;
                    my_staff_list();
                } else {
                    refreshLayout.setEnableLoadmore(false);

                }
//         }
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        list.clear();
        my_staff_list();
    }

    private void my_staff_list() {
        EasyHttp.post(this)
                .api(new MyStaffListApi().setPage(mPage).setUserid(SPUtil.get(this, IntentKey.USERID,0)))
                .request(new HttpCallback<StaffListBean>(this) {

                    @Override
                    public void onSucceed(StaffListBean data) {
                        flag = data.getFlag();
                        list.addAll(data.getData());
                        adapter.setNewData(list);
                        adapter.notifyDataSetChanged();

                    }
                });
    }

    @Override
    protected void initData() {


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
